import pygame
from pygame.locals import *
from Widget import *

class TextBox(Widget):
   def __init__(self, x, y, number_of_lines, chars_wide, font_, font_size):
      Widget.__init__(self)
      self.font = pygame.font.Font(font_, 12)
      self.rect = pygame.Rect(x, y, chars_wide*font_size,  self.font.get_linesize()*number_of_lines)
      self.num = number_of_lines
      self.buffer = []
      self.buffer_surfaces = []
      for s in range(self.num):
         self.add_line(str(s))
   def draw(self):
      
      pygame.draw.rect(self.screen, (95,95,95), 
         (  self.parent.rect.x+self.rect.x, 
            self.parent.rect.y+self.rect.y, 
            self.rect.w, self.rect.h+5))
      pygame.draw.rect(self.screen, (0,0,0), 
         (  self.parent.rect.x+self.rect.x+1, 
            self.parent.rect.y+self.rect.y+1, 
            self.rect.w-2, 
            self.rect.h-2+5))
      line = 0
      number_to_pull = len(self.buffer_surfaces) - self.num
      for s in self.buffer_surfaces[number_to_pull:]:
         labelX = self.getX() + 4
         labelY = (self.getY()+2) + line * self.font.get_linesize()
         self.screen.blit(s, ( labelX, labelY, self.rect.w,self.rect.h))
         line +=1

   def add_line(self, line):
      self.buffer.append(line)
      self.buffer_surfaces.append(self.font.render(line, 1, (255,255,255)))